const Counts = require('./Counts')



const getCounts=async (date) => {

    if(!date){
       date=getTodayDate()
    }

    let res=await Counts.findOne({date:date})

    return res

}
exports.getCounts=getCounts


const getTodayDate=function () {

    let datetime=new Date()

    let year=datetime.getFullYear()
    let month=datetime.getMonth()-0+1
    let day=datetime.getDate()
    let date=`${year}-${month}-${day}`

    return date
}
exports.getTodayDate=getTodayDate


const saveCounts=async (from_uid) => {
    let datetime=new Date()

    let year=datetime.getFullYear()
    let month=datetime.getMonth()-0+1
    let day=datetime.getDate()
    let date=`${year}-${month}-${day}`
    let time=datetime.getTime()

    let count_obj={
        date,
        year,
        month,
        day,
        time,
        uid_list:[from_uid]
    }

    let count=new Counts(count_obj)
    await count.save()

    return ''
}
exports.saveCounts=saveCounts




exports.addFakeMsg=async (datestr,total_num) => {
    let datetime=new Date(datestr)

    let year=datetime.getFullYear()
    let month=datetime.getMonth()-0+1
    let day=datetime.getDate()
    let date=`${year}-${month}-${day}`
    let time=datetime.getTime()

    let count_obj={
        date,
        year,
        month,
        day,
        time,
        total_num:total_num,
        uid_list:[
            2,
            3
        ]
    }

    let count=new Counts(count_obj)
    await count.save()

    return ''
}

exports.removeCount=async (day) => {
    let res=await Counts.remove({day:day})

    return res
}




exports.updateCounts=async (from_uid) => {

    let date=getTodayDate()

    let res=await getCounts(date)
    if(res){
        doUpdateCounts(res,date,from_uid)
    }
    else{
        saveCounts(from_uid)
    }

    return ''
}




const doUpdateCounts=async (res,date,from_uid) => {
    let num=res.total_num+1
    let uid_list=res.uid_list
    uid_list.push(from_uid)

    let updata_list = [...new Set(uid_list)];

    let new_obj=Object.assign(res,{total_num:num,uid_list:updata_list})
    await Counts.update({date:date},new_obj)

    return ''
}

exports.doUpdateCounts=doUpdateCounts


exports.getMonthNums=async (month) => {

    let datas=await Counts.find({month:month})

    let msg_nums=0
    let user_lists=[]

    for(let item of datas){

        msg_nums+=item.total_num-0
        user_lists=user_lists.concat(item.uid_list)
    }

    let user_set = [...new Set(user_lists)];

    return {
        msg_nums,
        user_lists:user_set
    }

}




exports.getNumsInSomeDay=async (start,end) => {

    let start_time=new Date(start).getTime()
    let end_time=new Date(end).getTime()+(24*3600-10)*1000


    let datas=await Counts.find({
        time:{
            $gte:start_time,
            $lte:end_time
        },

    }).sort('time')

    let arr=[]

    for(let item of datas){
        arr.push({
            date:item.date,
            total_msg_num:item.total_num,
            acitve_user_num:item.uid_list.length,
        })
    }


    //没有数据的，补全0
    let dateArr=await makeDateArr(start,end)
    let copy_arr=JSON.parse(JSON.stringify(arr))
    for(let item of dateArr){
        let is_in=IsdateInArr(item,copy_arr)
        if(!is_in){

            arr.push({
                date:item,
                total_msg_num:0,
                acitve_user_num:0,
            })
        }
    }

    let arr_sort=dateSort(arr)
    return arr_sort

}


const dateSort=function (arr) {
    let arr1=arr.sort(function (a,b) {
        let a_date=a.date
        let b_date=b.date
        let a_time=new Date(a_date).getTime()
        let b_time=new Date(b_date).getTime()
        return  maxTime(a_time,b_time)
    })

    return arr1
}



function maxTime(time1,time2) {
    let time_sec1=time1-0
    let time_sec2=time2-0
    if(time_sec1-time_sec2<0){
        return -1
    }
    if(time_sec1-time_sec2>0){
        return 1
    }
    return 0
}




const IsdateInArr=function (item,arr) {
   for(let obj of arr){
      if(obj.date==item){
          return true
      }
   }
   return false

}



const makeDateArr=async (start,end) => {
    let end_time=new Date(end).getTime()
    let begin_time=new Date(start).getTime()

    let cha=end_time-begin_time
    let day_time=3600*24*1000
    let day_cha=cha/day_time
    day_cha=parseInt(day_cha)+1

    let arr=[]
    for(let i=0; i<day_cha; i++){
        let this_time=begin_time+day_time*i;
        let date=formatDate(this_time)
        arr.push(date)
    }
    return arr
}
exports.makeDateArr=makeDateArr


const formatDate=function (this_time) {
    let timeStr=new Date(this_time)
    let year=timeStr.getFullYear()
    let month=timeStr.getMonth()+1
    let day=timeStr.getDate()
    let str=year+'-'+month+'-'+day

    return str
}







